package com.itfreer.arcgis.edit;

import android.content.DialogInterface;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureQueryResult;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.itfreer.arcgis.draw.DrawCommand;
import com.itfreer.arcgis.layer.LayerLoader;
import com.itfreer.arcgis.query.GetSelectFeatureListener;
import com.itfreer.arcgis.utils.FeatureTableUtils;
import com.itfreer.arcgis.utils.GeometryUtils;
import com.itfreer.arcgis.utils.LayerUtils;
import com.itfreer.gis.datasyn.entity.ProjectEntity;
import com.itfreer.gis.datasyn.sub.EditLogService;
import com.itfreer.utils.common.AlertUtils;
import com.itfreer.utils.common.ToastUtils;
import com.itfreer.utils.config.ConfigManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class DeleteSelectFeatureCommand implements DrawCommand
{
    @Override
    public void Execute(Map<String,Object> params)
    {
        final FeatureLayer featureLayer = EditToolUtils.getCurrentEditFeatureLayer();
        if (featureLayer == null) {
            ToastUtils.showShort(ConfigManager.getContext(), "当前无编辑图层！");
            return;
        }
        // 删除选中要素
        LayerUtils.GetSelectFeature(featureLayer, new GetSelectFeatureListener()
        {
            @Override
            public void execute(final FeatureLayer featureLayer, ListenableFuture<FeatureQueryResult> features)
            {
                final ProjectEntity config = LayerLoader.instance.getConfig(featureLayer);
                try
                {
                    final List<Feature> selectFeatures = new ArrayList<>();
                    for (Feature feature : features.get())
                        selectFeatures.add(feature);

                    if (selectFeatures.size() == 0)
                        ToastUtils.showShort(ConfigManager.getContext(), "当前编辑图层无选择要素");
                    else
                    {
                        AlertUtils.dialog(ConfigManager.getContext(), "系统提示", "确认删除选择的要素？", new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {
                                for (Integer i = 0; i < selectFeatures.size(); i++)
                                {
                                    Object sguid = selectFeatures.get(i).getAttributes().get("SGUID");
                                    Feature selectFeature = selectFeatures.get(0);
                                    Point centerPoint = selectFeature.getGeometry().getExtent().getCenter();
                                    //TODO song 解决图形删除后同步到服务端数据报错问题
                                    Geometry newGeometry= GeometryUtils.getCirclePolygon(centerPoint,0.0001,selectFeature.getGeometry().getSpatialReference());
                                    selectFeature.setGeometry(newGeometry);
                                    FeatureTableUtils.UpdateFeature(featureLayer.getFeatureTable(), selectFeatures.get(i));
                                    featureLayer.clearSelection();
                                    // 记录日志
                                    if (sguid != null)
                                    {
                                        EditLogService.instance.deleteLog(config, sguid.toString());
                                    }
                                }
                            }
                        }, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int which)
                            {

                            }
                        });
                    }
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                } catch (ExecutionException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
}